package de.fhtrier.gdw2.ships.states;

import java.io.IOException;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.loading.DeferredResource;
import org.newdawn.slick.loading.LoadingList;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

import de.fhtrier.gdw2.ships.utils.AssetLoader;

public class LoadGameState extends BasicGameState
{
    private MainMenuState menuState;
    private GameplayState gameState;
    private DeferredResource nextResource;
    
    @Override
    public void init(GameContainer container, StateBasedGame game) throws SlickException
    {
        LoadingList.setDeferredLoading(false);
        AssetLoader.getInstance();
		
        menuState = new MainMenuState();
        menuState.init(container, game);
        game.addState(menuState);
        
        gameState = new GameplayState();
        gameState.init(container, game);
        game.addState(gameState);
    }

    @Override
    public void render(GameContainer container, StateBasedGame game, Graphics g) throws SlickException
    {
        container.setShowFPS(false);
        g.fillRect(0, 0, container.getWidth(), container.getHeight());
    }

    @Override
    public void update(GameContainer container, StateBasedGame game, int delta) throws SlickException
    {
            if(nextResource != null)
            {
                try 
                {
                    nextResource.load();
                } 
                catch (IOException e) 
                {
                    throw new SlickException("Failed to load: "+nextResource.getDescription(), e);
                }
                nextResource = null;
            }
    
        if (LoadingList.get().getRemainingResources() > 0)
        {
            nextResource = LoadingList.get().getNext();
        } 
        else
        {        
            container.setShowFPS(true);
            game.enterState(0);
        }
}

    @Override
    public int getID()
    {
        return 2;
    }

}
